package com.yunbao.ioc.factory;

import com.yunbao.ioc.tx.TransactionCglibAdvisor;
import com.yunbao.ioc.tx.TransactionJdkAdvisor;
import com.yunbao.ioc.tx.TransactionManager;
import net.sf.cglib.proxy.Enhancer;
import java.lang.reflect.Proxy;

/**
 * Created by louisyuu on 2020/3/4 下午2:39
 */
public class ProxyFactory {


    private TransactionManager transactionManager;

    public TransactionManager getTransactionManager() {
        return transactionManager;
    }

    public void setTransactionManager(TransactionManager transactionManager) {
        this.transactionManager = transactionManager;
    }

    /**
     * Jdk动态代理
     *
     * @param target 委托对象
     * @return 代理对象
     */
    public Object getJdkProxy(Object target) {

        // 获取代理对象
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new TransactionJdkAdvisor(transactionManager, target));

    }


    /**
     * 使用cglib动态代理生成代理对象
     *
     * @param target 委托对象
     * @return
     */
    public Object getCglibProxy(Object target) {
        return Enhancer.create(target.getClass(), new TransactionCglibAdvisor(transactionManager,target));
    }
}
